-- proj: Mission Server
-- date: 2020/1/2
-- auth: Jason

local require 	= require
local pairs		= pairs
local ngx 		= ngx

local t_cache = ngx.shared.misCache

if not ngx.b_misLoad then
	package.path = package.path .. ';lua/modules/mission/?.lua;lua/?.lua'
	package.cpath = package.cpath .. ";lua/modules/commhm/resty/?.so;";
end

-- require级入口
-- require统一在这里执行，在其他文件做进一步缓存
-- 简化文件包含关系，防止相互包含问题
if not ngx.t_misModules then
	ngx.t_misModules = {
		mis_config = require('mis_config');
		mis_log = require('mis_log');
		mis_common = require('mis_common');
		mis_calcu = require('mis_calcu');
		mis_data = require('mis_data');
		mis_center = require('mis_center');
		mis_act = require('mis_act');
		mis_user = require('mis_user');
		mis_bridge = require('mis_bridge');
		mis_mysql = require('mis_mysql');
		mis_condition = require('mis_condition');
		mis_cmd = require('mis_cmd');
		mis_qps = require('mis_qps');
		mis_lock = require('mis_lock');
		mis_saveAss = require('mis_saveAss');
		mis_network = require("modules.commhm.network");
		mis_weekendMail = require('mis_weekendMail');
		mis_extend = require('mis_extend');
		mis_annualVote = require('mis_annualVote');
		mis_allowIp = require('mis_allowIp');
		mis_weekendPush = require('mis_weekendPush');
		mis_awakening = require('mis_awakening');
		mis_boat_festival = require('mis_boat_festival');
		mis_douluodalu = require("mis_douluodalu");
		mis_viptheme = require('mis_viptheme');
		mis_mapShare = require('mis_mapShare');
		mis_sanrio = require('mis_sanrio');
		mis_moon = require('mis_moon');
		mis_currency = require('mis_currency');
	}
	for _, v in pairs(ngx.t_misModules) do
		if v.modules_include then
			v.modules_include(ngx.t_misModules)
		end
	end
end
local t_modules = ngx.t_misModules

-- 紧急通道
if not ngx.t_misEmergencyTab then
	ngx.t_misEmergencyTab = {
		t_modules.mis_data
	}
end
for _, v in pairs(ngx.t_misEmergencyTab) do
	local n_code, s_desc = v.emergency()
	if n_code ~= t_modules.mis_config.t_retCode.n_OK then
		s_desc = 'call ' .. v.s_name .. ' emergency failed, ' .. s_desc
		t_modules.mis_log.error(s_desc)
		t_modules.mis_bridge.send(n_code, s_desc)
		return
	end
end

-- nginx级入口
if not ngx.b_misOnce then
	-- 进入纵向临界区
	local n_workerId = ngx.worker.id()
	ngx.b_misOnce = t_cache:get('b_misOnce' .. n_workerId)
	if not ngx.b_misOnce then
		-- 进入横向临界区
		local n_code, t_lock = t_modules.mis_lock.lock('enter_once')
		if n_code ~= t_modules.mis_config.t_retCode.n_OK then
			t_modules.mis_bridge.send(t_modules.mis_config.t_retCode.n_SYS_ERR, t_lock)
			return
		end
		if not ngx.b_misOnce then
			if t_modules.mis_center.enter_once() then
				local b_success, s_err = t_cache:set('b_misOnce' .. n_workerId, true)
				if not b_success then
					local s_msg = 'set ngx.shared.cache failed, ' .. s_err
					t_modules.mis_log.error(s_msg)
					t_modules.mis_bridge.send(t_modules.mis_config.t_retCode.n_SYS_ERR, s_msg)
					return
				end
				ngx.b_misOnce = true
			else
				t_modules.mis_lock.unlock(t_lock)
				return
			end
		end
		t_modules.mis_lock.unlock(t_lock)
	end
end

-- load级入口
if not ngx.b_misLoad then
	local n_code, t_lock = t_modules.mis_lock.lock('enter_load')
	if n_code ~= t_modules.mis_config.t_retCode.n_OK then
		t_modules.mis_bridge.send(t_modules.mis_config.t_retCode.n_SYS_ERR, t_lock)
		return
	end
	if not ngx.b_misLoad then
		if t_modules.mis_center.enter_load() then
			ngx.b_misLoad = true
		else
			t_modules.mis_lock.unlock(t_lock)
			return
		end
	end
	t_modules.mis_lock.unlock(t_lock)
end

-- request级入口
t_modules.mis_center.enter_request()

-- 析构入口
t_modules.mis_center.enter_finish()